#include <iostream>
#include <thread>
#include "../../common/detail.hpp"
#include "../../client/rpc_client.hpp"

void onJsonrsp(const Json::Value &result){
    std::cout << "result3: " << result.asInt() << std::endl;
}

int main(){
    myrpc::initLoggers();

    auto client = std::make_shared<myrpc::client::RpcClient>(false, "127.0.0.1", 28080);

    // 同步调用
    Json::Value params1, result1;
    params1["num1"] = 11;
    params1["num2"] = 22;
    bool ret = client->call("Add", params1, result1);
    if (ret != false){
        std::cout << "result1: " << result1.asInt() << std::endl;
    }
    std::this_thread::sleep_for(std::chrono::seconds(2));
    // 异步调用
    Json::Value params2, result2;
    params2["num1"] = 58;
    params2["num2"] = 12;
    myrpc::client::RpcCaller::JsonRspFuture res_future;
    ret = client->call("Add", params2, res_future);
    if (ret != false){
        result2 = res_future.get();
        std::cout << "result2: " << result2.asInt() << std::endl;
    }
    std::this_thread::sleep_for(std::chrono::seconds(2));
    // 回调调用
    Json::Value params3;
    params3["num1"] = 40;
    params3["num2"] = 77;
    ret = client->call("Add", params3, onJsonrsp);

    std::this_thread::sleep_for(std::chrono::seconds(10));

   
    mylog::shutdown();
    return 0;
}












void message_test(){
    // Rpc请求测试
    // myrpc::RpcRequest::ptr req1 = myrpc::MessageFactory::create<myrpc::RpcRequest>();
    // req1->setMethod("Add");
    // Json::Value parameters;
    // parameters["num1"] = 11;
    // parameters["num2"] = 22;
    // req1->setParameters(parameters);
    // std::string str1 = req1->serialize();
    // std::cout << str1 << std::endl;

    // myrpc::BaseMessage::ptr bmp = myrpc::MessageFactory::create(myrpc::MsgType::REQ_RPC);
    // bool ret1 = bmp->unserialize(str1);
    // myrpc::RpcRequest::ptr req2 = std::dynamic_pointer_cast<myrpc::RpcRequest>(bmp);
    // req2->check();
    // std::cout << req2->getMethod() << std::endl;
    // Json::Value ret = req2->getParameters();
    // std::cout << ret["num1"] << " " << ret["num2"] << std::endl;

    // 主题请求测试
    // myrpc::TopicRequest::ptr req1 = myrpc::MessageFactory::create<myrpc::TopicRequest>();
    // req1->setTopicKey("音乐");
    // req1->setTopicOptype(myrpc::TopicOpType::TOPIC_PUBLISH);
    // req1->setTopicMsg("新歌上线!");
    // std::string str = req1->serialize();
    // std::cout << str << std::endl;

    // myrpc::BaseMessage::ptr bmp = myrpc::MessageFactory::create(myrpc::MsgType::REQ_TOPIC);
    // bmp->unserialize(str);
    // myrpc::TopicRequest::ptr req2 = std::dynamic_pointer_cast<myrpc::TopicRequest>(bmp);
    // req2->check();
    // std::cout << req2->getTopicKey() << " " << static_cast<int>(req2->getTopicOpType()) << " " << std::endl;
    // std::cout << req2->getTopicMsg() << std::endl;

    // 服务请求测试
    myrpc::ServiceRequest::ptr req3 = myrpc::MessageFactory::create<myrpc::ServiceRequest>();
    req3->setMethod("get_user_list");
    req3->setServiceOptype(myrpc::ServiceOpType::SERVICE_REGISTRY);
    myrpc::Address addr = {"127.0.0.1", 8080};
    req3->setHost(addr);
    std::string str3 = req3->serialize();
    std::cout << "Service Request Serialized: " << str3 << std::endl;

    myrpc::BaseMessage::ptr bmp3 = myrpc::MessageFactory::create(myrpc::MsgType::REQ_SERVICE);
    bmp3->unserialize(str3);
    myrpc::ServiceRequest::ptr req4 = std::dynamic_pointer_cast<myrpc::ServiceRequest>(bmp3);
    req4->check();
    std::cout << "Service Request Deserialized: " << req4->getMethod() << ", " << static_cast<int>(req4->getServiceOpType()) << std::endl;
    myrpc::Address host_ret = req4->getHost();
    std::cout << "Host: " << host_ret.first << ":" << host_ret.second << std::endl;

    // Rpc响应测试
    myrpc::RpcResponse::ptr rsp1 = myrpc::MessageFactory::create<myrpc::RpcResponse>();
    rsp1->setRCode(myrpc::RCode::RCODE_OK);
    Json::Value result1;
    result1["sum"] = 33;
    rsp1->setResult(result1);
    std::string str4 = rsp1->serialize();
    std::cout << "Rpc Response Serialized: " << str4 << std::endl;

    myrpc::BaseMessage::ptr bmp4 = myrpc::MessageFactory::create(myrpc::MsgType::RSP_RPC);
    bmp4->unserialize(str4);
    myrpc::RpcResponse::ptr rsp2 = std::dynamic_pointer_cast<myrpc::RpcResponse>(bmp4);
    rsp2->check();
    std::cout << "Rpc Response Deserialized: RCode=" << static_cast<int>(rsp2->getRCode()) << std::endl;
    std::cout << "Result: " << rsp2->getResult().toStyledString() << std::endl;

    // 主题响应测试 (此类没有额外字段，只测试基类字段)
    myrpc::TopicResponse::ptr rsp3 = myrpc::MessageFactory::create<myrpc::TopicResponse>();
    rsp3->setRCode(myrpc::RCode::RCODE_OK);
    std::string str5 = rsp3->serialize();
    std::cout << "Topic Response Serialized: " << str5 << std::endl;

    myrpc::BaseMessage::ptr bmp5 = myrpc::MessageFactory::create(myrpc::MsgType::RSP_TOPIC);
    bmp5->unserialize(str5);
    myrpc::TopicResponse::ptr rsp4 = std::dynamic_pointer_cast<myrpc::TopicResponse>(bmp5);
    rsp4->check();
    std::cout << "Topic Response Deserialized: RCode=" << static_cast<int>(rsp4->getRCode()) << std::endl;

    // 服务响应测试 (服务发现场景)
    myrpc::ServiceResponse::ptr rsp5 = myrpc::MessageFactory::create<myrpc::ServiceResponse>();
    rsp5->setRCode(myrpc::RCode::RCODE_OK);
    rsp5->setServiceOptype(myrpc::ServiceOpType::SERVICE_DISCOVERY);
    rsp5->setMethod("get_user_list");
    std::vector<myrpc::Address> hosts_to_set;
    hosts_to_set.push_back({"192.168.1.1", 9000});
    hosts_to_set.push_back({"192.168.1.2", 9001});
    rsp5->setHosts(hosts_to_set);
    std::string str6 = rsp5->serialize();
    std::cout << "Service Response Serialized: " << str6 << std::endl;
    
    myrpc::BaseMessage::ptr bmp6 = myrpc::MessageFactory::create(myrpc::MsgType::RSP_SERVICE);
    bmp6->unserialize(str6);
    myrpc::ServiceResponse::ptr rsp6 = std::dynamic_pointer_cast<myrpc::ServiceResponse>(bmp6);
    rsp6->check();
    std::cout << "Service Response Deserialized: RCode=" << static_cast<int>(rsp6->getRCode()) << ", Optype=" << static_cast<int>(rsp6->getServiceOpType()) << ", Method=" << rsp6->getMethod() << std::endl;
    std::vector<myrpc::Address> hosts_ret = rsp6->getHosts();
    std::cout << "Hosts found:" << std::endl;
    for (const auto& h : hosts_ret) {
        std::cout << "  - " << h.first << ":" << h.second << std::endl;
    }
}